<?php
/**
 * Created by PhpStorm.
 * User: 王松
 * Time: 2017/6/12 13:34
 */

namespace App\Transformers;

use App\Models\MedicalAuxiliary;
use App\Models\MedicalFollowUp;
use App\Models\MedicalRecord;
use App\Models\Patient;
use League\Fractal\ParamBag;
use League\Fractal\TransformerAbstract;

class RecordTransformer extends TransformerAbstract
{
    protected $availableIncludes = [
        'patient', 'hospital', 'doctor', 'complaint', 'hpi', 'past','person',
        'marriage','family','physical','auxiliary','diagnose','dispose','follow'
    ];

//    private $validParams = ['with', 'info', 'need', 'print'];

    /**
     * @param \App\Models\MedicalRecord $record
     * @return array
     */
    public function transform(MedicalRecord $record)
    {
        //  病历信息

        return [
            'id'              => $record->id,
            'patient_id'      => $record->patient_id,
            'hospital_id'     => $record->hospital_id,
            'doctor_id'       => $record->doctor_id,
            'clinic_at'       => $record->clinic_at,
            'medication'      => $record->medication,
            'cn_medication'   => $record->cn_medication,
            'interval'        => $record->interval,
            'visit_type'      => $record->visit_type,
            'updated_by'      => $record->updated_by,
            'created_at'      => $record->created_at->toDateTimeString(),
            'updated_at'      => $record->updated_at->toDateTimeString(),
        ];
    }

    /**
     * 返回信息中加入诊断的详细信息
     * @param \App\Models\MedicalRecord $record
     * @param \League\Fractal\ParamBag  $bag
     * @return \League\Fractal\Resource\Item
     */
    public function includePatient(MedicalRecord $record, ParamBag $bag)
    {
        $print = $bag->get('print')[0];

        if ($print) {
            $patient = Patient::where(['id' => $record->patient_id])->first([
                'name', 'sex', 'age', 'medical_number', 'diabetes_type'
            ]);

            return $this->item($patient, function (Patient $patient) {return $patient->toArray();});
        }

        return $this->item(
            $record->patient,
            (new PatientEditTransformer())->setDefaultIncludes((array)$bag->get('include'))
        );
    }

    /**
     * 返回信息中加入医院的信息
     * @param \App\Models\MedicalRecord $record
     * @param \League\Fractal\ParamBag  $bag
     * @return \League\Fractal\Resource\Item
     */
    public function includeHospital(MedicalRecord $record, ParamBag $bag)
    {
        $need = $bag->get('need')[0] ? : 'simple';

        return $this->item($record, function (MedicalRecord $record) use ($need) {
                $select = ['id', 'name'];

                switch ($need) {
                    case 'detail' :
                        $select = ['*'];
                }

                $hospital = $record->hospital()->getQuery()->select($select)->first();
                if ($hospital) {
                    return $hospital->toArray();
                }

                return [];
            }
        );
    }

    /**
     * 返回信息中加入医院的信息
     * @param \App\Models\MedicalRecord $record
     * @param \League\Fractal\ParamBag  $bag
     * @return \League\Fractal\Resource\Item
     */
    public function includeDoctor(MedicalRecord $record, ParamBag $bag)
    {
        $need = $bag->get('need')[0] ? : 'simple';

        return $this->item($record, function (MedicalRecord $record) use ($need) {

            $select = ['id', 'name'];

            switch ($need) {
                case 'detail' :
                    $select = ['*'];
            }

            $doctor = $record->doctor()->getQuery()->select($select)->first();
            if ($doctor) {
                return $doctor->toArray();
            }

            return [];
        });
    }

    /**
     * 返回信息中加入主诉的信息
     * @param \App\Models\MedicalRecord $record
     * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
     */
    public function includeComplaint(MedicalRecord $record)
    {
        if ($complaint = $record->complaint) {
            return $this->item($complaint, new ComplaintTransformer());
        }

        return $this->null();
    }

    /**
     * 返回信息中加入现病史的信息
     * @param \App\Models\MedicalRecord $record
     * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
     */
    public function includeHpi(MedicalRecord $record)
    {
        if ($hpi = $record->hpi) {
            return $this->item($hpi, new HpiTransformer());
        }

        return $this->null();
    }

    /**
     * 返回信息中加入既往史的信息
     * @param \App\Models\MedicalRecord $record
     * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
     */
    public function includePast(MedicalRecord $record)
    {
        if ($past = $record->past) {
            return $this->item($past, new PastHistoryTransformer());
        }

        return $this->null();
    }

    /**
     * 返回信息中加入个人史的信息
     * @param \App\Models\MedicalRecord $record
     * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
     */
    public function includePerson(MedicalRecord $record)
    {
        if ($person = $record->person) {
            return $this->item($person, new PersonalHistoryTransformer());
        }

        return $this->null();
    }

    /**
     * 返回信息中加入婚姻史的信息
     * @param \App\Models\MedicalRecord $record
     * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
     */
    public function includeMarriage(MedicalRecord $record)
    {
        if ($marriage = $record->marriage) {
            return $this->item($marriage, new MarriageHistoryTransformer());
        }

        return $this->null();
    }

    /**
     * 返回信息中加入家族史的信息
     * @param \App\Models\MedicalRecord $record
     * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
     */
    public function includeFamily(MedicalRecord $record)
    {
        if ($family = $record->family) {
            return $this->item($family, new FamilyHistoryTransformer());
        }

        return $this->null();
    }

    /**
     * 返回信息中加入体格检查的信息
     * @param \App\Models\MedicalRecord $record
     * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
     */
    public function includePhysical(MedicalRecord $record)
    {
        if ($physical = $record->physical) {
            return $this->item($physical, new MedicalPhysicalTransformer());
        }

        return $this->null();
    }

    /**
     * 返回信息中加入辅助检查的信息
     * @param \App\Models\MedicalRecord $record
     * @param \League\Fractal\ParamBag  $bag
     * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
     */
    public function includeAuxiliary(MedicalRecord $record, ParamBag $bag)
    {
        $print = $bag->get('print')[0];

        if ($print) {
            $auxiliary = (new MedicalAuxiliary())->printItems($record);

            $transform = function (MedicalAuxiliary $auxiliary) {return $auxiliary->toArray();};
        } else {
            $auxiliary = $record->auxiliary;

            $transform = new MedicalAuxiliaryTransformer();
        }

        if ($auxiliary) {
            return $this->item($auxiliary, $transform);
        }

        return $this->null();
    }

    /**
     * 返回信息中加入诊断的信息
     * @param \App\Models\MedicalRecord $record
     * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
     */
    public function includeDiagnose(MedicalRecord $record)
    {
        if ($diagnose = $record->diagnose) {
            return $this->item($diagnose, new MedicalDiagnoseTransformer());
        }

        return $this->null();
    }

    /**
     * 返回信息中加入处理的信息
     * @param \App\Models\MedicalRecord $record
     * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
     */
    public function includeDispose(MedicalRecord $record)
    {
        if ($dispose = $record->dispose) {
            return $this->item($dispose, new DisposeTransformer());
        }

        return $this->null();
    }

    /**
     * 返回信息中加入随诊的信息
     * @param \App\Models\MedicalRecord $record
     * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
     */
    public function includeFollow(MedicalRecord $record)
    {
        if ($record->isFirstVisit()) {
            return $this->null();
        }

        $follow = MedicalFollowUp::where(['record_id' => $record->id])->first();

        if ($follow) {
            return $this->item($follow, new FollowUpTransformer());
        }

        return $this->null();
    }
}